package com.example.demo.hcontroller;

import com.example.demo.pojo.Topic;
import com.example.demo.pojo.Tpaper;
import com.example.demo.service.ITopicService;
import com.example.demo.service.ITpaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/hTopic")
public class HTopicController {

    @Autowired
    private ITpaperService TpaperService;

    @Autowired
    private ITopicService TopicService;
    @RequestMapping("/toTopic")
    private String toTopic(Model model, Topic Topic) {
        List<Tpaper> tlist = TpaperService.selectTpaper();
        model.addAttribute("tpaperlist", tlist);
        List<Topic> list = TopicService.selectTopic();
        model.addAttribute("topiclist", list);
        return "topic";
    }

    @RequestMapping("/toaddTopic")
    private String toaddTopic(Model model, Topic Topic) {
        List<Tpaper> tlist = TpaperService.selectTpaper();
        model.addAttribute("tpaperlist", tlist);
        return "addtopic";
    }

    @RequestMapping("/addTopic")
    private String addTopic( Topic Topic, String title, Integer tid) throws IOException {

        Topic.setTitle(title);
        Topic.setTid(1);
        Topic.setIcon(0);
        TopicService.insertSelective(Topic);
        return "redirect:toTopic";
    }

    @RequestMapping("/toupdateTopic")
    private String toupdateTopic(Model model, Topic Topic, Integer id) {
        model.addAttribute("topic", TopicService.selectByPrimaryKey(id));
        return "updatetopic";
    }

    @RequestMapping("/updateTopic")
    private String updateTopic(Model model, Topic Topic, String analysis, Integer id, String ifc, String leixing, String title, Integer tid) {
        Topic.setTitle(title);
        Topic.setId(id);
        TopicService.updateByPrimaryKeySelective(Topic);
        return "redirect:toTopic";
    }

    @RequestMapping("/deleteTopic")
    private String deleteTopic(Model model, Topic Topic, Integer id) {
        TopicService.deleteByPrimaryKey(id);
        return "redirect:toTopic";
    }
}
